from xml.etree import ElementTree
from C_Header_Parse.Struct_Class_Define import headfile,struct,array,member,define

# user config block




def Gen_XML_Base_Header(RTE_FileName,HeadFile):
    #创建根元素
    root = ElementTree.Element("AUTOSAR")
    # 嘿嘿
    root.attrib["xmlns"] = "http://autosar.org/schema/r4.0"
    root.attrib["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
    root.attrib["xsi:schemaLocation"] = "http://autosar.org/schema/r4.0 AUTOSAR_4-3-1.xsd"

    #  先搭一下xml的基础框架
    Element = HeadFile.Gen_XML_Frame(root,RTE_FileName)

    # 结构体相关的生成代码
    for st_index in HeadFile.Struct:
        # 如果结构体里面有数组，那么需要优先去设置数组的xml结构
        '''  eg:
        <IMPLEMENTATION-DATA-TYPE>
            <SHORT-NAME>CAM_IhcsDatauint8_Tpadding3</SHORT-NAME>
            <CATEGORY>ARRAY</CATEGORY>
            <SUB-ELEMENTS>
            <IMPLEMENTATION-DATA-TYPE-ELEMENT>
                <SHORT-NAME>CAM_IhcsDatauint8_Tpadding3__</SHORT-NAME>
                <CATEGORY>TYPE_REFERENCE</CATEGORY>
                <ARRAY-SIZE>3</ARRAY-SIZE>
                <ARRAY-SIZE-SEMANTICS>FIXED-SIZE</ARRAY-SIZE-SEMANTICS>
                <SW-DATA-DEF-PROPS>
                <SW-DATA-DEF-PROPS-VARIANTS>
                    <SW-DATA-DEF-PROPS-CONDITIONAL>
                    <IMPLEMENTATION-DATA-TYPE-REF DEST="IMPLEMENTATION-DATA-TYPE">/AUTOSAR_Platform/ImplementationDataTypes/uint8</IMPLEMENTATION-DATA-TYPE-REF>
                    </SW-DATA-DEF-PROPS-CONDITIONAL>
                </SW-DATA-DEF-PROPS-VARIANTS>
                </SW-DATA-DEF-PROPS>
            </IMPLEMENTATION-DATA-TYPE-ELEMENT>
            </SUB-ELEMENTS>
        </IMPLEMENTATION-DATA-TYPE>
        '''
        # 结构体中有数组成员时，需要先建立数组类型的元素树，之后在结构体的元素树中做引用

        if len(HeadFile.Struct[st_index].Member_Array) > 0:
            for Array_index in HeadFile.Struct[st_index].Member_Array:
                Array_Implementation_Data_Type = ElementTree.SubElement(Element,"IMPLEMENTATION-DATA-TYPE")
                Array_Short_Name = ElementTree.SubElement(Array_Implementation_Data_Type,"SHORT-NAME")
                Array_Short_Name.text = HeadFile.Struct[st_index].Member_Array[Array_index].Type + HeadFile.Struct[st_index].Member_Array[Array_index].Name + HeadFile.Struct[st_index].Name
                Array_Category = ElementTree.SubElement(Array_Implementation_Data_Type,"CATEGORY")
                Array_Category.text = 'ARRAY'
                Array_Sub_Elements = ElementTree.SubElement(Array_Implementation_Data_Type,"SUB-ELEMENTS")
                Array_Implementation_Data_Type_Element = ElementTree.SubElement(Array_Sub_Elements,"IMPLEMENTATION-DATA-TYPE-ELEMENT")
                Array_DataType_Short_Name = ElementTree.SubElement(Array_Implementation_Data_Type_Element,"SHORT-NAME")
                Array_DataType_Short_Name.text = "typedef_" + Array_Short_Name.text
                Array_Datatype_Category = ElementTree.SubElement(Array_Implementation_Data_Type_Element,"CATEGORY")
                Array_Datatype_Category.text = 'TYPE_REFERENCE'
                Array_Datatype_Array_Size = ElementTree.SubElement(Array_Implementation_Data_Type_Element,"ARRAY-SIZE")
                Array_Datatype_Array_Size.text = HeadFile.Struct[st_index].Member_Array[Array_index].Size
                Array_Datatype_Array_Size_Semantics = ElementTree.SubElement(Array_Implementation_Data_Type_Element,"ARRAY-SIZE-SEMANTICS")
                Array_Datatype_Array_Size_Semantics.text = 'FIXED-SIZE'
                Array_Sw_Data_Def_Props = ElementTree.SubElement(Array_Implementation_Data_Type_Element,"SW-DATA-DEF-PROPS")
                Array_Sw_Data_Def_Props_Variants = ElementTree.SubElement(Array_Sw_Data_Def_Props,"SW-DATA-DEF-PROPS-VARIANTS")
                Array_Sw_Data_Def_Props_Conditional = ElementTree.SubElement(Array_Sw_Data_Def_Props_Variants,"SW-DATA-DEF-PROPS-CONDITIONAL")
                #Array_Sw_Calibration_Access = ElementTree.SubElement(Array_Sw_Data_Def_Props_Conditional,"SW-CALIBRATION-ACCESS")
                #Array_Sw_Calibration_Access.text = 'READ-WRITE'

                Implementation_Data_Type_Ref = ElementTree.SubElement(Array_Sw_Data_Def_Props_Conditional,"IMPLEMENTATION-DATA-TYPE-REF",attrib={"DEST": "IMPLEMENTATION-DATA-TYPE"})
                if True == HeadFile.Struct[st_index].Member_Array[Array_index].BaseTypeJudge(HeadFile.Struct[st_index].Member_Array[Array_index].TpyeJuge()):
                    #Implementation_Data_Type_Ref = ElementTree.SubElement(Array_Sw_Data_Def_Props_Conditional,"IMPLEMENTATION-DATA-TYPE-REF",attrib={"DEST": "SW-BASE-TYPE"})
                    Implementation_Data_Type_Ref.text = '/AUTOSAR_PlatformTypes/ImplementationDataTypes/' + HeadFile.Struct[st_index].Member_Array[Array_index].TpyeJuge()
                else:
                    #Implementation_Data_Type_Ref = ElementTree.SubElement(Array_Sw_Data_Def_Props_Conditional,"IMPLEMENTATION-DATA-TYPE-REF",attrib={"DEST": "IMPLEMENTATION-DATA-TYPE"})
                    Implementation_Data_Type_Ref.text = f"/{RTE_FileName}/DataTypes/ " + HeadFile.Struct[st_index].Member_Array[Array_index].TpyeJuge()


                #Array_Sw_Impl_Policy = ElementTree.SubElement(Array_Sw_Data_Def_Props_Conditional,"SW-IMPL-POLICY")
                #Array_Sw_Impl_Policy.text = 'STANDARD'

        Category_text = 'STRUCTURE'
        Sub_Elements = HeadFile.Struct[st_index].Gen_Struct_Start(Element,st_index,Category_text)
        # 遍历单个结构体的所有成员
        for Member_index in HeadFile.Struct[st_index].Member:
            Sub_Element_DataType = ElementTree.SubElement(Sub_Elements,"IMPLEMENTATION-DATA-TYPE-ELEMENT")
            Member_ShortName = ElementTree.SubElement(Sub_Element_DataType,"SHORT-NAME")
            Member_ShortName.text = HeadFile.Struct[st_index].Member[Member_index].Name
            # 可能需要扩展 union todo
            Category_ShortName = ElementTree.SubElement(Sub_Element_DataType,"CATEGORY")
            Category_ShortName.text = 'TYPE_REFERENCE'
            # 给结构体成员添加类型
            '''  eg.
                                    <SW-DATA-DEF-PROPS>
                                        <SW-DATA-DEF-PROPS-VARIANTS>
                                            <SW-DATA-DEF-PROPS-CONDITIONAL>
                                                <IMPLEMENTATION-DATA-TYPE-REF DEST="IMPLEMENTATION-DATA-TYPE">/vehinfo/DataTypes/Boolean</IMPLEMENTATION-DATA-TYPE-REF>
                                            </SW-DATA-DEF-PROPS-CONDITIONAL>
                                        </SW-DATA-DEF-PROPS-VARIANTS>
                                    </SW-DATA-DEF-PROPS>'''
            Sub_Element_DataType_Ref = ElementTree.SubElement(Sub_Element_DataType,"SW-DATA-DEF-PROPS")
            Sub_Element_DataType_VARIANTS = ElementTree.SubElement(Sub_Element_DataType_Ref,"SW-DATA-DEF-PROPS-VARIANTS")
            Sub_Element_DataType_Confitional = ElementTree.SubElement(Sub_Element_DataType_VARIANTS,"SW-DATA-DEF-PROPS-CONDITIONAL")
            if True == HeadFile.Struct[st_index].Member[Member_index].BaseTypeJudge(HeadFile.Struct[st_index].Member[Member_index].TpyeJuge()):
                Sub_Element_DataType_imp_Ref = ElementTree.SubElement(Sub_Element_DataType_Confitional,"IMPLEMENTATION-DATA-TYPE-REF",attrib={"DEST": "SW-BASE-TYPE"})
                Sub_Element_DataType_imp_Ref.text = '/AUTOSAR_PlatformTypes/ImplementationDataTypes/' + HeadFile.Struct[st_index].Member[Member_index].TpyeJuge()
            else:
                Sub_Element_DataType_imp_Ref = ElementTree.SubElement(Sub_Element_DataType_Confitional,"IMPLEMENTATION-DATA-TYPE-REF",attrib={"DEST": "IMPLEMENTATION-DATA-TYPE"})
                Sub_Element_DataType_imp_Ref.text = f"/{RTE_FileName}/DataTypes/ " + HeadFile.Struct[st_index].Member[Member_index].TpyeJuge()



    # 序列化为XML字符串（可选，如果只需要内存中的XML字符串）
    xml_str = ElementTree.tostring(root, encoding="unicode", method="xml", xml_declaration=True)

    # 或者直接写入文件（推荐，将XML保存到磁盘）
    ElementTree.indent(root)
    tree = ElementTree.ElementTree(root)
    tree.write("RNTEC_DataType.arxml", encoding="utf-8", xml_declaration=True, method="xml")